package net.xdclass.exception;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.util.JsonData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
//@RestControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData handler(Exception e) {
        if(e instanceof BizException){
            BizException bizExeption = (BizException) e;
            return JsonData.buildCodeAndMsg(bizExeption.getCode(), bizExeption.getMsg());
        }else{
            log.error("【系统异常】{}", e);
            return JsonData.buildError("系统异常");
        }
    }
}
